const debounce = function (fn, delay) {
    var timeId = null;
    return function (...args) {
        if (timeId) {
            clearTimeout(timeId);
        }
        timeId = setTimeout(() => {
            fn.apply(this, args);
            timeId = null;
        }, delay);
    };
};

// const throttle = function (fn, delay) {
//     var canRun = true;
//     return function (...args) {
//         if (!canRun) {
//             return;
//         }
//         canRun = false;
//         setTimeout(() => {
//             fn.apply(this, args);
//             canRun = true;
//         }, delay);
//     };
// };

const throttle = function (fn, delay) {
    var last = null;
    return function (...args) {
        var canRun = false;
        var now = Date.now();
        if (!last || now - last >= delay) {
            last = now;
            canRun = true;
        }
        if (!canRun) {
            return;
        }
        fn.apply(this, args);
    };
};

var func = function () {
    console.log('----func------')
}
var func2 = function () {
    console.log('----func2------')
}

let t1 = throttle(func, 100);
let t2 = throttle(func2, 50);

// let t1 = debounce(func, 100);
// let t2 = debounce(func2, 20);
var run = function () {
    Array.from(new Array(10)).forEach((_, i) => {
        setTimeout(t1, i * 10);
        setTimeout(t2, i * 10);
        // func();
        // func2();
    });
};

run();

